﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace FotoAlbum
{
    /// <summary>
    /// The utility class contains some generally useful methods.
    /// </summary>
    static class Utility
    {
        /// <summary>
        /// Get the file info of all the files that match the given extension.
        /// </summary>
        /// <param name="dir"></param>
        /// <param name="extensions"></param>
        /// <returns>The file infos of files matching one of the given extensions.</returns>
        public static IEnumerable<FileInfo> GetFilesByExtensions(this DirectoryInfo dir, params string[] extensions)
        {
            if (extensions == null)
                throw new ArgumentNullException("extensions");
            var files = dir.EnumerateFiles();
            return files.Where(f => extensions.Contains(f.Extension.ToLower()));
        }

        /// <summary>
        /// Set the use of a double buffer to redraw the control using a secondary buffer; reduces or even prevents flickering.
        /// </summary>
        /// <param name="control"></param>
        /// <param name="enable">true when a secondary buffer should be used; false otherwise.</param>
        public static void SetDoubleBuffered(this Control control, bool enable)
        {
            var pi = control.GetType().GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(control, enable, null);
        }
    }
}
